package com.example.schoolnewsapp.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.schoolnewsapp.R;
import com.example.schoolnewsapp.database.DBHelper;

public class RegisterActivity extends AppCompatActivity {
    private ImageView imageView;
    private DBHelper dbHelper;
    private TextView register;
    private EditText username, password, repassword;
    private CheckBox checkBox;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        imageView = (ImageView) findViewById(R.id.reg_back);

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RegisterActivity.this, LoginOrRegisterActivity.class);
                startActivity(intent);
                finish();
            }
        });


        dbHelper = new DBHelper(this, "UserDB.db", null, 6);
        register = (TextView) findViewById(R.id.reg_sub);
        username = (EditText) findViewById(R.id.reg_user);
        password = (EditText) findViewById(R.id.reg_pass);
        repassword = (EditText) findViewById(R.id.reg_repass);
        checkBox = (CheckBox) findViewById(R.id.reg_check);


        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                     SQLiteDatabase db = dbHelper.getWritableDatabase();
                     String username_str = username.getText().toString().trim();
                     String password_str = password.getText().toString().trim();
                     String repassword_str = repassword.getText().toString().trim();
                     if(!TextUtils.isEmpty(username_str) && !TextUtils.isEmpty(password_str)&& !TextUtils.isEmpty(repassword_str)){
                         if (checkBox.isChecked()){
                            if (password_str.equals(repassword_str)) {
                                ContentValues values = new ContentValues();
                                values.put("name", username_str);
                                values.put("password", password_str);
                                db.insert("User", null, values);
                                startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
                                finish();
                                Toast.makeText(RegisterActivity.this, "验证通过，注册成功", Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(RegisterActivity.this, "两次密码不一致，请重新输入", Toast.LENGTH_SHORT).show();
                            }
                         } else {
                             Toast.makeText(RegisterActivity.this, "请勾选隐私政策", Toast.LENGTH_SHORT).show();
                         }
                     }else {
                         Toast.makeText(RegisterActivity.this, "未完善信息，注册失败", Toast.LENGTH_SHORT).show();
                     }
                            db.close();
                        }

            });
        }


}